import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    meta: { show: true ,num:0},
    name: "Home",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/Home.vue"),
    children: [
      {
        path: "/home/popup",
        name: "SearchPopup",
        component: () =>
          import(
            /* webpackChunkName: "SearchPopup" */ "../views/SearchPopup.vue"
          ),
      },
    ],
  },
  {
    path: "/subject",
    meta: { show: true ,
    num:1},
    name: "subject",
    component: () =>
      import(/* webpackChunkName: "subject" */ "../views/subject.vue"),
  },
  {
    path: "/cart",
    meta: { show: true,num:2 },
    name: "cart",
    component: () => import(/* webpackChunkName: "cart" */ "../views/cart.vue"),
  },
  {
    path: "/cate",
    meta: { show: true,num:3 },
    name: "cate",
    component: () => import(/* webpackChunkName: "cate" */ "../views/cate.vue"),
  },
  {
    path: "/mine",
    meta: { show: true,num:4 },
    name: "mine",
    component: () => import(/* webpackChunkName: "mine" */ "../views/mine.vue"),
  },
  {
    path: "/channel",
    name: "channel",
    component: () =>
      import(/* webpackChunkName: "channel" */ "@/components/Home/channel.vue"),
  },
  {
    path: "/brand/:id",
    name: "brand",
    component: () =>
      import(/* webpackChunkName: "brand */ "@/components/Home/brand.vue"),
  },
  {
    path: "/details/:id",
    name: "details",
    component: () =>
      import(/* webpackChunkName: "details */ "@/components/Home/details.vue"),
  },
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});
// 路由守卫
router.beforeEach((to, from, next) => {
  // 判断是否登录
  if (to.path === "/cart") {
    let token = localStorage.getItem('token')
    // 如果有token表示登录,进入
    if (token) {
      next()
      // 未登录跳转我的页面
    } else {
      next('aa')
      setTimeout(() => {
        next('/mine')
      }, 800);
    }
  }
  next()
})



export default router;
